<script type="text/javascript">
	function sendAjax(value) {
		//show loading panel in jsloading.jsp
		showLoading();

		arr = value.split(',');
		flightId = arr[0];
		index = arr[1];
		checkSubscribe = arr[2];
		if (checkSubscribe == -1) {
			var json = {
				"flightId" : flightId + "",
				"carriers" : jsonAirlines,
				"stopsFilter" : $('#txtnumStop').val() + "",
				"departMin": $("#departMin").val() + "",
				"departMax": $("#departMax").val() + "",
				"returnMin": $("#returnMin").val() + "",
				"returnMax": $("#returnMax").val() + "",
				"durationMin": $("#durationMin").val() + "",
				"durationMax": $("#durationMax").val() + ""
				
			};

			$.ajax({
						url : "${pageContext.request.contextPath}/flight/subscribe.html",
						data : JSON.stringify(json),
						type : 'POST',
						dataType : "text",
						beforeSend : function(xhr) {
							xhr.setRequestHeader("Accept", "application/json");
							xhr.setRequestHeader("Content-Type",
									"application/json");
						},
						success : function(string) {
// 							$.each(response, function(idx, obj) {
// 								if(obj.flightId === flightId){
// 									obj.checkSubscribe = 1;
// 									return false;
// 								}
// 							});
							window.location.href = "${pageContext.request.contextPath}/account/login.html";
						},
						error : function(string, status, er) {
							$.confirm({
								'title'		: 'SUBSCRIBE',
								'message'	: 'Error happened!',
								'buttons'	: {
									'OK'	: {
										'action': function(){
											$("#confirmBox").slideUp();
										}	// Nothing to do in this case. You can as well omit the action property.
									}
								}
							});
						}
					});
		} else {
			sub = "Subscribe";
			unsub = "Unsubscribe";
			sub_class = "button subscribe right a-subscribe";
			unsub_class = "button unsubscribe right a-subscribe";

			if ($("#flight" + index).attr("name") == sub) {
				
				controller_url = "${pageContext.request.contextPath}/flight/subscribe.html";
				message = "You subscribed this flight successfully !";

			} else if ($("#flight" + index).attr("name") == unsub) {
				
				controller_url = "${pageContext.request.contextPath}/subscribe/unsubscribe.html";
				message = "You unsubscribed this flight successfully !";
			}

			var json = {
				"flightId" : flightId + "",
				"carriers" : jsonAirlines,
				"stopsFilter" : $('#txtnumStop').val() + "",
				"departMin": $("#departMin").val() + "",
				"departMax": $("#departMax").val() + "",
				"returnMin": $("#returnMin").val() + "",
				"returnMax": $("#returnMax").val() + "",
				"durationMin": $("#durationMin").val() + "",
				"durationMax": $("#durationMax").val() + ""
			};

			$.ajax({
				
				url : controller_url,
				data : JSON.stringify(json),
				type : 'POST',
				dataType : "text",
				beforeSend : function(xhr) {
					xhr.setRequestHeader("Accept", "application/json");
					xhr.setRequestHeader("Content-Type", "application/json");
				},
				success : function(successMessage) {
					
					// Change checkSubscribe after click 'Subscribe' button
					$.each(response, function(idx, obj) {
						if(obj.flightID == flightId){
							if ($("#flight" + index).attr("name") == sub) {
								obj.checkSubscribe = 1;
							}else{
								obj.checkSubscribe = 0;
							}
							return false;
						}
					});
					unShowLoading();
					
					// Display popup success message (bottom)
					generate('success', message);
					
					// Change color of button after subscribe successfully
					if ($("#flight" + index).attr("name") == sub) {

						$("#flight" + index + " img").attr("src", '${pageContext.request.contextPath}/resources/img/ic_unsubscribe.png');
						$("#flight" + index + " span").html(unsub);
						$("#flight" + index).attr("name", unsub);
						$("#flight" + index).attr("class", unsub_class);

					} else if ($("#flight" + index).attr("name") == unsub) {

						$("#flight" + index + " img").attr("src", '${pageContext.request.contextPath}/resources/img/ic_subscribe.png');
						$("#flight" + index + " span").html(sub);
						$("#flight" + index).attr("name", sub);
						$("#flight" + index).attr("class", sub_class);
					}
				},
				error : function(string, status, er) {
					$.confirm({
						'title'		: 'SUBSCRIBE',
						'message'	: 'Error happened!',
						'buttons'	: {
							'OK'	: {
								'action': function(){
									$("#confirmBox").slideUp();
								}	// Nothing to do in this case. You can as well omit the action property.
							}
						}
					});
				}
			});
		}
		event.preventDefault();
	}
</script>